# Replication code for Hall and Snyder PSRM
# Note that this code requires DTAs generating in Stata replication code
# Date: 9/2/14

# Set working directory here
setwd("C:/Users/Babak/Desktop/Hall")
library(foreign)


#### Function to make Figures 1, 2, 3

make.plot <- function(data, filename, type=1) {
	
	

	pdf(file=filename, height=5, width=7, compress=T)
	
	par(mfrow=c(2,2), mar=c(1,3,1,2), oma=c(3,3,3,3))
	
	plot(x=data$rv, y=data$next_qual_R, xaxt="n", xlab="", yaxt="n", ylab="", col="white")
	points(x=data$rv, y=data$next_qual_R, cex=.7, col="gray45")
	R <- vector(length=length(unique(data$bin)))
		D <- vector(length=length(unique(data$bin)))
	W <- vector(length=length(unique(data$bin)))

	count <- 1
	for (i in unique(data$bin)) {
		R[count] <- data$mean_R[which(data$bin==i)][1]
		D[count] <- data$mean_D[which(data$bin==i)][1]
		W[count] <- data$mean_win[which(data$bin==i)][1]
		count <- count + 1
	}
	data$bin <- data$bin + 0.25
	axis(side=2, las=1)
	mtext(side=2, "Pre-Existing Experience", line=3.8, cex=.8)
	mtext(side=2, "of Repub Candidate, t+1", line=2.5, cex=.8)
	points(x=data$rv, y=data$y, pch=16, cex=.7, col="black")
	abline(v=0, lty=2, col="black")
		points(x=unique(data$bin), y=R, col="red", pch=16)

	text(x=2.7, y=.91, "Challenging Party", cex=.8)
	text(x=2.7, y=.81, "Candidate Exp", cex=.8)
	
	
	
	plot(x=data$rv, y=data$next_pct_D, xlab="", yaxt="n", ylab="", col="white", xaxt="n", ylim=c(30,70))
	points(x=data$rv, y=data$next_pct_D, col="gray45", cex=.7)
	points(x=data$rv, y=data$y_share, pch=16, cex=.7, col="black")

	axis(side=2, las=1)
	mtext(side=2, "Dem Vote Share, t+1", line=2.5, cex=.8)
	abline(v=0, lty=2, col="black")
	
	plot(x=data$rv, y=data$next_qual_D, xlab="", yaxt="n", ylab="", col="white")
	points(x=data$rv, y=data$next_qual_D, cex=.7, col="gray45")
	axis(side=2, las=1)
	mtext(side=2, "Pre-Existing Experience", line=3.8, cex=.8)
	mtext(side=2, "of Dem Candidate, t+1", line=2.5, cex=.8)
	points(x=data$rv, y=data$y_d, pch=16, cex=.7, col="black")
	points(x=unique(data$bin), y=D, col="red", pch=16)

	abline(v=0, lty=2, col="black")
	mtext(side=1, "Democratic Win Margin, time t", line=2, cex=.8)
	text(x=-2.7, y=.85, "Challenging Party", cex=.8)
	text(x=-2.7, y=.75, "Candidate Exp", cex=.8)
	
	plot(x=data$rv, y=data$next_win_D, xlab="", yaxt="n", ylab="", col="white")
	points(x=data$rv, y=data$next_win_D, col="gray45", cex=.7)
	points(x=data$rv, y=data$y_win, pch=16, cex=.7, col="black")
	axis(side=2, las=1)
	points(x=unique(data$bin), y=W, col="red", pch=16)

	mtext(side=2, "Dem Win, t+1", line=2.5, cex=.8)
	abline(v=0, lty=2, col="black")
	mtext(side=1, "Democratic Win Margin, time t", line=2, cex=.8)
	
	
	
	dev.off()
}

data <- read.dta("tmp_rdd_us_statewide_R.dta")
make.plot(data, "Statewide_qual.pdf")

data <- read.dta("tmp_rdd_us_house_period_2_R.dta")
make.plot(data, "House_qual.pdf")

data <- read.dta("tmp_rdd_stleg_R.dta")
make.plot(data, "StLeg_qual.pdf", 0)


### Code to make Figure 4


setwd("C:/Users/Babak/Desktop/Hall")
library(foreign)


data <- read.dta("downstream_r_graph.dta")

pdf(file="downstream.pdf")
plot(x=1:4, y=data$B2, col="white", xlab="Terms Downstream", ylab="Estimated Net Scare-off", xaxt="n", yaxt="n", ylim=c(-.75, .75), cex.lab=1.3)
abline(h=0, col="red", lty=2)
segments(x0=1:4, x1=1:4, y0=data$B3, y1=data$B4, lwd=2, col="gray30")
points(x=1:4, y=data$B2, pch=16, cex=2)
axis(side=2, las=1, cex.axis=1.3)
axis(side=1, at=1:4, labels=1:4, cex.axis=1.3)
dev.off()





#### Code for Figures A1, A2, A3
setwd("C:/Users/Babak/Desktop/Hall")
rm(list=ls())
library(foreign)
data <- read.dta("for_robust_graph1.dta")
attach(data)
lw<-4

pdf("rdd_robust_statewide.pdf", height=6, width=10)
plot(x=B5, y=B1, type="l", lwd=lw, ylim=c(-.2,.2), col="pink", xlab="RDD Margin", ylab="Estimated Effect on Net Quality Difference, t+1", lty=1, cex.lab=1.3, cex.main=1.4, xlim=c(4,25), xaxt="n")
axis(side=1, at=seq(5,50,5), labels=seq(5,50,5))
lines(x=B5, y=B2, type="l", lwd=lw, col="black", lty=2)
lines(x=B5, y=B3, type="l", lwd=lw, col="darkgreen", lty=3)
lines(x=B5, y=B4, type="l", lwd=lw, col="purple", lty=4)
legend("bottomright", c("Linear", "Quadratic", "Cubic", "Quartic"), lty=c(1,2,3,4), col=c("pink", "black", "darkgreen", "purple"))
dev.off()

rm(list=ls())
data <- read.dta("for_robust_graph2.dta")
attach(data)
lw<-4

pdf("rdd_robust_house.pdf", height=6, width=10)
plot(x=B5, y=B1, type="l", lwd=lw, ylim=c(0,.3), col="pink", xlab="RDD Margin", ylab="Estimated Effect on Net Quality Difference, t+1", lty=1, cex.lab=1.3, cex.main=1.4, xlim=c(4,25), xaxt="n")
axis(side=1, at=seq(5,50,5), labels=seq(5,50,5))
lines(x=B5, y=B2, type="l", lwd=lw, col="black", lty=2)
lines(x=B5, y=B3, type="l", lwd=lw, col="darkgreen", lty=3)
lines(x=B5, y=B4, type="l", lwd=lw, col="purple", lty=4)
legend("bottomright", c("Linear", "Quadratic", "Cubic", "Quartic"), lty=c(1,2,3,4), col=c("pink", "black", "darkgreen", "purple"))

dev.off()

rm(list=ls())
data <- read.dta("for_robust_graph3.dta")
attach(data)
lw<-4

pdf("rdd_robust_stleg.pdf", height=6, width=10)
plot(x=B5, y=B1, type="l", lwd=lw, ylim=c(-.1,.3), col="pink", xlab="RDD Margin", ylab="Estimated Effect on Net Quality Difference, t+1", lty=1, cex.lab=1.3, cex.main=1.4, xlim=c(4,25), xaxt="n")
axis(side=1, at=seq(5,50,5), labels=seq(5,50,5))

lines(x=B5, y=B2, type="l", lwd=lw, col="black", lty=2)
lines(x=B5, y=B3, type="l", lwd=lw, col="darkgreen", lty=3)
lines(x=B5, y=B4, type="l", lwd=lw, col="purple", lty=4)
legend("bottomright", c("Linear", "Quadratic", "Cubic", "Quartic"), lty=c(1,2,3,4), col=c("pink", "black", "darkgreen", "purple"))

dev.off()


### Code for Figures A4, A5, A6



setwd("C:/Users/Babak/Desktop/Hall")
rm(list=ls())
library(foreign)
data <- read.dta("for_balance_graph1.dta")
attach(data)
lw<-4



rm(list=ls())
data <- read.dta("for_balance_graph2.dta")
attach(data)
lw<-4

pdf("rdd_balance_house.pdf", height=6, width=10)
plot(x=B4, y=B1, type="l", lwd=lw, ylim=c(-.4,.4), col="black", xlab="RDD Margin", ylab="Estimate", lty=1, cex.lab=1.3, cex.main=1.4, xlim=c(4,25), main="")
lines(x=B4, y=B2, lty=2)
lines(x=B4, y=B3, lty=2)
abline(h=0, col="red")

dev.off()


rm(list=ls())
data <- read.dta("for_balance_graph3.dta")
attach(data)
lw<-4

pdf("rdd_balance_stleg.pdf", height=6, width=10)
plot(x=B4, y=B1, type="l", lwd=lw, ylim=c(-.4,.4), col="black", xlab="RDD Margin", ylab="Estimate", lty=1, cex.lab=1.3, cex.main=1.4, xlim=c(4,25), main="")
lines(x=B4, y=B2, lty=2)
lines(x=B4, y=B3, lty=2)
abline(h=0, col="red")

dev.off()


